<?php
/**
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\model;


use think\Model;
use think\Db;

class AdminPermissions extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'admin_permissions';
     
    /**
     * [所有权限]
     * @return [type] [description]
     */
    public function permissionsAll(){
        $ob = Db::table("admin_permissions")
            ->field([
                "id",
                "title",
                "path",
                "method",
                "activity",
                "remarks",
                "pid",
            ]);
        $info = $ob
            ->select();
        return $info;
    } 

    /**
     * [角色的所有权限]
     * @return [type] [description]
     */
    public function permissionsByRoleid($roleId){
        $ob = Db::table("admin_rolepermissions releation")
            ->join("admin_permissions permisson","releation.permission_id= permisson.id")
            ->where("releation.role_id",$roleId);
        $info = $ob
            ->field([
                "releation.role_id as roleId",
                "releation.permission_id as permissionId",
                "permisson.title",
                "permisson.path",
                "permisson.method",
                "permisson.activity",
                "permisson.remarks",
                "permisson.pid",
            ])
            ->select();
        return $info;
    }
    /**
     * [addOnePermission description]
     * @param [type] $userId [description]
     * @param [type] $data   [description]
     */
    public function addOnePermission($userId,$data){
        $roleId = $data['roleId'];
        foreach ($data['permissions'] as $key => $value) {
            // try{
            // if(empty($value['id'])){
                //添加
                $ret = Db::table("admin_permissions")
                ->insert([
                    'id'=>$value['id'],
                    'title'=>$value['title'],
                    'path'=>$value['path'],
                    'activity'=>1,
                    // 'method'=>$value['method'],
                    // 'remarks'=>$value['remarks'],
                    'pid'=>$value['pid']
                ]);
                // echo $ret;exit;
            // }
            // else{
            //     //编辑 
            //     Db::table("admin_permissions")
            //     ->where("id",$value['id'])
            //     ->update([
            //         'title'=>$value['title'],
            //         'path'=>$value['path'],
            //         'activity'=>1,
            //         // 'method'=>$value['method'],
            //         // 'remarks'=>$value['remarks'],
            //         'pid'=>$value['pid']
            //     ]);
            // }

        }
        return 0;
    }
}